package com.pasabahceambalaj.session;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import org.jboss.seam.annotations.In;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.framework.EntityHome;

import com.pasabahceambalaj.entity.Firma;
import com.pasabahceambalaj.entity.Kalip;
import com.pasabahceambalaj.entity.Kalipaltkalip;
import com.pasabahceambalaj.entity.Olcubirim;

@Name("kalipHome")
public class KalipHome extends EntityHome<Kalip> {

	@In(create = true)
	FirmaHome firmaHome;
	@In(create = true)
	OlcubirimHome olcubirimHome;

	private Kalip selectedAltKalip;
	private Integer altKalipAdet;

	public void setKalipId(Long id) {
		setId(id);
	}

	public Long getKalipId() {
		return (Long) getId();
	}

	@Override
	protected Kalip createInstance() {
		Kalip kalip = new Kalip();
		return kalip;
	}

	public void load() {
		if (isIdDefined()) {
			wire();
		}
	}
	
	
	public void wire() {
		getInstance();
		
		Firma firma = null;
		if (Authenticator.isAdmin()) {
			firma = firmaHome.getDefinedInstance();
		} else {
			firma = Authenticator.getFirma();
		}

		if (firma != null) {
			getInstance().setFirma(firma);
		}
		
		Olcubirim olcubirim = olcubirimHome.getDefinedInstance();
		if (olcubirim != null) {
			getInstance().setOlcubirim(olcubirim);
		}
	}

	public boolean isWired() {
		if (getInstance().getFirma() == null)
			return false;
		return true;
	}

	public Kalip getDefinedInstance() {
		return isIdDefined() ? getInstance() : null;
	}

	public List<Kalipaltkalip> getKalipAltKalipList() {
		return getInstance() == null ? null : new ArrayList<Kalipaltkalip>(getInstance().getKalipAltKalipList());
	}

	public void addAltKalip() {
		if (getSelectedAltKalip() != null) {
			if (getAltKalipAdet() != null) {
				Kalipaltkalip selectedKalipaltkalip = contains(getSelectedAltKalip());
				if (selectedKalipaltkalip != null) {
					selectedKalipaltkalip.setAltkalipadet(selectedKalipaltkalip.getAltkalipadet() + getAltKalipAdet());
				} else {
					Kalipaltkalip kalipaltkalip = new Kalipaltkalip();
					kalipaltkalip.setKalip(this.instance);
					kalipaltkalip.setAltKalip(getSelectedAltKalip());
					kalipaltkalip.setAltkalipadet(getAltKalipAdet());
					this.instance.getKalipAltKalipList().add(kalipaltkalip);
				}
			}
		}
		setSelectedAltKalip(null);
		setAltKalipAdet(null);
	}

	public void removeAltKalip(Kalipaltkalip kalipaltkalip) {
		if (kalipaltkalip != null) {
			this.instance.getKalipAltKalipList().remove(kalipaltkalip);
		}
	}

	private Kalipaltkalip contains(Kalip selectedAltKalip) {
		Set<Kalipaltkalip> set = this.instance.getKalipAltKalipList();
		for (Kalipaltkalip kalipaltkalip : set) {
			if (kalipaltkalip.getAltKalip().getKod().equals(selectedAltKalip.getKod())) {
				return kalipaltkalip;
			}
		}
		return null;
	}

	public void setSelectedAltKalip(Kalip selectedAltKalip) {
		this.selectedAltKalip = selectedAltKalip;
	}

	public Kalip getSelectedAltKalip() {
		return selectedAltKalip;
	}

	public void setAltKalipAdet(Integer altKalipAdet) {
		this.altKalipAdet = altKalipAdet;
	}

	public Integer getAltKalipAdet() {
		return altKalipAdet;
	}

}